﻿using Authentication.Model.Interface;
using Common.Contract.Authentication.Record;
using Common.Contract.Authentication.Request;
using Common.Contract.Authentication.Response;
using Common.Contract.Paging;
using Common.Contract.SystemConfig.Record;
using Common.Service.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Authentication.Core.ServiceCommand
{
    public class RetrieveListMsboServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveListMsboRequest, RetrieveListMsboResponse>
    {

        public int pageSize = 10, pageIndex = 0, totalRecord = 0;
        public string TextSearch;
        public FilterRecord SearchColumns = null;
        public RetrieveListMsboServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveListMsboRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (Request.CurrentUser == null)
            {
                throw new ArgumentException("Dao must be set", "request");
            }
            SearchColumns = Request.Filter;
        }

        protected override bool OnExecute(RetrieveListMsboResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            int pageSize = 0, pageIndex = 0, totalRecord = 0;
            var applicationKey = Request.Header.ApplicationKey;
            try
            {
                if (Request.Page != null)
                {
                    pageIndex = Request.Page.PageIndex ?? 0;
                    pageSize = Request.Page.PageSize ?? 0;
                }
                var listUser = Providers.Users.RetrieveListUserByDao(out totalRecord, Request.CurrentUser, SearchColumns, pageSize, pageIndex);
                if (listUser == null)
                {
                    response.ErrorMessage = string.Format("Can not found user with Dao={0}", Request.CurrentUser.Dao);
                    response.IsSuccess = false;
                }
                List<UserRecord> records = listUser.Select(x => new UserRecord
                {
                    UserName = x.UserName,
                    Dao = x.Dao,
                    ContractType = x.PositionName,
                    Branch = x.Branch == null ? string.Empty : x.Branch.BranchNameSme,
                    StartWorkingDate = x.DateSalesStart.HasValue ? x.DateSalesStart.Value.ToShortDateString() : DateTime.Now.ToShortDateString(),
                    Zone = x.Branch == null ? string.Empty : x.Branch.Region,
                    BranchCode = x.BranchCodeSme,
                    BranchId = x.Branch == null ? string.Empty : x.Branch.BranchId,
                    Email = x.Email,
                    FullName = x.DaoName,
                    Team = x.Team
                }).ToList();

                response.Records = records;
                response.Page = new PageRecord
                {
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    TotalRecords = totalRecord,
                };
            }
            catch (Exception e)
            {
                Console.Write(e.Message);
                response.ErrorMessage = string.Format("Can not found user with Dao={0}", Request.CurrentUser.Dao);
                response.IsSuccess = false;
            }
            return isSuccess;
        }
    }
}
